/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.shop.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.thinkgem.jeesite.modules.cms.entity.BusinessRepairs;
import com.thinkgem.jeesite.modules.sys.entity.Office;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.security.SystemAuthorizingRealm;
import com.thinkgem.jeesite.modules.sys.service.SystemService;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.shop.entity.BusinessCategory;
import com.thinkgem.jeesite.modules.shop.service.BusinessCategoryService;

import java.security.Principal;
import java.util.List;
import java.util.Map;

/**
 * 商品分类Controller
 * @author 开发者
 * @version 2016-07-06
 */
@Controller
@RequestMapping(value = "${adminPath}/shop/businessCategory")
public class BusinessCategoryController extends BaseController {

	@Autowired
	private BusinessCategoryService businessCategoryService;

    @Autowired
    private SystemService systemService;


	@ModelAttribute
	public BusinessCategory get(@RequestParam(required=false) String id) {
		BusinessCategory entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = businessCategoryService.get(id);
		}
		if (entity == null){
			entity = new BusinessCategory();
		}
		return entity;
	}

    @RequiresPermissions("shop:businessCategory:view")
    @RequestMapping(value = {""})
    public String index(BusinessCategory businessCategory, Model model) {
//        model.addAttribute("list", businessCategoryService.findList(businessCategory));
        return "modules/shop/businessCategoryIndex";
    }

	@RequiresPermissions("shop:businessCategory:view")
	@RequestMapping(value = {"list"})
	public String list(BusinessCategory businessCategory, Model model) {
        model.addAttribute("list", businessCategoryService.findList(businessCategory,true));
		return "modules/shop/businessCategoryList";
	}

	@RequiresPermissions("shop:businessCategory:view")
	@RequestMapping(value = "form")
	public String form(BusinessCategory businessCategory, Model model) {
        if(businessCategory.getParent() != null && businessCategory.getParent().getId() != null){
            businessCategory.setParent(businessCategoryService.get(businessCategory.getParent().getId()));
        }
		model.addAttribute("businessCategory", businessCategory);
		return "modules/shop/businessCategoryForm";
	}

	@RequiresPermissions("shop:businessCategory:edit")
	@RequestMapping(value = "save")
	public String save(BusinessCategory businessCategory, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, businessCategory)){
			return form(businessCategory, model);
		}
		businessCategoryService.save(businessCategory);
		addMessage(redirectAttributes, "保存信息成功");
        String id = "0".equals(businessCategory.getParentId()) ? "" : businessCategory.getParentId();
		return "redirect:"+Global.getAdminPath()+"/shop/businessCategory/list?id="+id+"&parentIds="+businessCategory.getParentIds();
	}
	
	@RequiresPermissions("shop:businessCategory:edit")
	@RequestMapping(value = "delete")
	public String delete(BusinessCategory businessCategory, RedirectAttributes redirectAttributes) {
		businessCategoryService.delete(businessCategory);
		addMessage(redirectAttributes, "删除信息成功");
//        String id = "0".equals(businessCategory.getParentId()) ? "" : businessCategory.getParentId();
        return "redirect:"+Global.getAdminPath()+"/shop/businessCategory/list?id=" +businessCategory.getId()+"&parentIds="+businessCategory.getParentIds();
	}

    @ResponseBody
    @RequestMapping(value = "treeData")
    public List<Map<String, Object>> treeData(@RequestParam(required=false) String extId, HttpServletResponse response) {
        List<Map<String, Object>> mapList = Lists.newArrayList();
        BusinessCategory businessCategory = new BusinessCategory();
        List<BusinessCategory> list = businessCategoryService.findList(businessCategory);
        for (int i=0; i<list.size(); i++){
            BusinessCategory e = list.get(i);
            if ((StringUtils.isBlank(extId) || (extId!=null && !extId.equals(e.getId()) && e.getParentIds().indexOf(","+extId+",")==-1))){
                Map<String, Object> map = Maps.newHashMap();
                map.put("id", e.getId());
                map.put("pId", e.getParentId());
                map.put("pIds", e.getParentIds());
                map.put("name", e.getName());
//                if (type != null && "3".equals(type)){
//                    map.put("isParent", true);
//                }
                mapList.add(map);
            }
        }
        return mapList;
    }

    @RequiresPermissions("shop:businessCategory:view")
    @RequestMapping(value = "tree")
    public String classifiedInfo(Model model) {
        SystemAuthorizingRealm.Principal principal = UserUtils.getPrincipal();
//        Office office = new Office();
        User user = systemService.getUser(principal.getId());
        List<BusinessCategory> list = businessCategoryService.findByParentId("1");
        model.addAttribute("categoryList", list);
        if(user.getUserType().equals("4")){
            model.addAttribute("businessId",user.getId());
        }else{
            model.addAttribute("businessId","");
        }
        return "modules/shop/businessProductTree";
    }

}